package com.ngm.tea.t.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.ModulePrivilegeDao;
import com.ngm.tea.t.dao.RoleDao;
import com.ngm.tea.t.dao.RolePrivilegeDao;
import com.ngm.tea.t.entity.ModulePrivilege;
import com.ngm.tea.t.entity.Role;
import com.ngm.tea.t.entity.RolePrivilege;
import com.ngm.tea.t.service.RoleService;

/**
 * 服务层
 * 
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

	@Resource
	private RoleDao roleDao;
	@Resource
	RolePrivilegeDao rolePrivilegeDao;
	@Resource
	private ModulePrivilegeDao modulePrivilegeDao;

	public RoleDao getRoleDao() {
		return roleDao;
	}

	@Override
	public void insert(Role role) {
		roleDao.insert(role);
	}

	@Override
	public void deleteById(Long id) {
		roleDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			roleDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Role role) {
		roleDao.delete(role);
	}

	@Override
	public void update(Role role) {
		roleDao.update(role);
	}

	@Override
	public Role selectOne(Long id) {
		return roleDao.selectOne(id);
	}

	@Override
	public List<Role> selectAll() {
		return roleDao.selectAll();
	}

	@Override
	public List<Role> selectList(Role role, PageInfo pageInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams", makeStr(role));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField() + " " + pageInfo.getOrderDirection());
		return roleDao.selectList(map);
	}

	@Override
	public Long count(Role role) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams", makeStr(role));
		return roleDao.count(map);
	}

	public String makeStr(Role role) {
		StringBuffer sb = new StringBuffer();
		if (!StringUtils.isEmpty(role.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + role.getId() + "%'");
			} else {
				sb.append(" AND id LIKE '%" + role.getId() + "%'");
			}
		}
		if (!StringUtils.isEmpty(role.getRoleCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("roleCode LIKE '%" + role.getRoleCode() + "%'");
			} else {
				sb.append(" AND roleCode LIKE '%" + role.getRoleCode() + "%'");
			}
		}
		if (!StringUtils.isEmpty(role.getRoleName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("roleName LIKE '%" + role.getRoleName() + "%'");
			} else {
				sb.append(" AND roleName LIKE '%" + role.getRoleName() + "%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	private void clean(String roleCode) {
		Map<String, String> map = new HashMap<>();
		map.put("roleCode", roleCode);
		List<Map<String, Object>> list = rolePrivilegeDao.selectOneByRoleCode(map);
		List<String> mlist = new ArrayList<>();
		for (Map<String, Object> rp : list) {
			String mp_id = rp.get("mp_id") + "";
			mlist.add(mp_id);
		}
		if (mlist.size() > 0) {
			modulePrivilegeDao.deleteByIds(mlist);
		}
	}

	@Override
	public void setAuth(String json, String roleCode) throws Exception {
		clean(roleCode);
		ObjectMapper mapper = new ObjectMapper();
		List<HashMap<String, String>> list = mapper.readValue(json, List.class);
		for (HashMap<String, String> map : list) {
			String modCode = map.get("modCode");
			String priCode = map.get("priCode");
			ModulePrivilege modulePrivilege = new ModulePrivilege();
			modulePrivilege.setModCode(modCode);
			modulePrivilege.setPriCode(priCode);
			if (modulePrivilegeDao.insert(modulePrivilege) == 1) {
				RolePrivilege rolePrivilege = new RolePrivilege();
				rolePrivilege.setMpId(modulePrivilege.getId());
				rolePrivilege.setRoleCode(roleCode);
				rolePrivilegeDao.insert(rolePrivilege);
			}
		}
	}

}